package com.example.martin1e.ui.dashboard;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.example.martin1e.databinding.FragmentDashboardBinding;

public class DashboardFragment extends Fragment {

    private FragmentDashboardBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        DashboardViewModel dashboardViewModel = new DashboardViewModel();
        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        // 设置偶像选择Spinner的监听器
        binding.id12.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedIdol = parent.getItemAtPosition(position).toString();
                updateIdolInfo(selectedIdol);
                Toast.makeText(getContext(), "选择偶像: " + selectedIdol, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // 什么都不做
            }
        });

        // 设置职业选择Spinner的监听器
        binding.id23.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedCareer = parent.getItemAtPosition(position).toString();
                // 处理职业选择
                Toast.makeText(getContext(), "选择职业: " + selectedCareer, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // 什么都不做
            }
        });

        return root;
    }

    private void updateIdolInfo(String selectedIdol) {
        // 根据选择的偶像更新信息
        if (binding != null) {
            String career = getIdolCareer(selectedIdol);
            String background = getIdolBackground(selectedIdol);
            binding.id22.setText("偶像职业：" + career);
            binding.id32.setText("偶像出生：" + background);
            Toast.makeText(getContext(), 
                "偶像: " + selectedIdol + "\n职业: " + career + "\n背景: " + background, 
                Toast.LENGTH_LONG).show();
        }
    }

    private String getIdolCareer(String idol) {
        // 这里可以根据实际需求返回偶像的职业信息
        switch (idol) {
            case "马云":
            case "任正非":
            case "乔布斯":
            case "马斯克":
                return "企业家";
            case "毛泽东":
            case "周恩来":
            case "邓小平":
            case "孙中山":
                return "政治家";
            case "鲁迅":
            case "李白":
                return "文学家";
            case "巴菲特":
                return "投资家";
            case "钱学森":
            case "牛顿":
            case "爱因斯坦":
                return "科学家";
            case "贝多芬":
                return "艺术家";
            case "老子":
            case "孔子":
            case "释迦摩尼":
            case "耶稣":
                return "思想家";
            default:
                return "未知";
        }
    }

    private String getIdolBackground(String idol) {
        // 这里可以根据实际需求返回偶像的背景信息
        switch (idol) {
            case "马云":
                return "普通教师";
            case "任正非":
                return "农村家庭";
            case "马斯克":
                return "中产家庭";
            case "乔布斯":
                return "被收养家庭";
            case "毛泽东":
                return "农民家庭";
            case "周恩来":
                return "官宦家庭";
            case "邓小平":
                return "地主家庭";
            case "孙中山":
                return "农民家庭";
            case "鲁迅":
                return "没落官宦";
            case "李白":
                return "富商家庭";
            case "巴菲特":
                return "股票经纪人家庭";
            case "钱学森":
                return "知识分子家庭";
            case "牛顿":
                return "农民家庭";
            case "爱因斯坦":
                return "中产犹太家庭";
            case "贝多芬":
                return "音乐家庭";
            case "老子":
            case "孔子":
                return "贵族家庭";
            default:
                return "未知";
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}